﻿

namespace CommonHelp
{
    using System;
    using System.IO;
    using System.Windows.Forms;

    public class FileHelper
    {
        public static string SaveFile(string filter)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = filter;
            DialogResult diaRst = sfd.ShowDialog();

            if (diaRst == DialogResult.OK)
            {
                return sfd.FileName;
            }

            return string.Empty;
        }
        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string ReadFile(string filePath)
        {
            if (!File.Exists(filePath))
            {
                return null;
            }

            string ret = string.Empty;
            TextReader rd = new StreamReader(filePath);
            ret = rd.ReadToEnd();
            rd.Close();
            return ret;
        }

        public static string ChoseDir()
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            DialogResult diaRst = folderBrowserDialog.ShowDialog();

            if (diaRst != DialogResult.OK)
            {
                return string.Empty;
            }

            return folderBrowserDialog.SelectedPath;
        }

        public static bool CopyFile(string sourceFileName, string destFileName)
        {
            try
            {
                DirectoryInfo dirInfo = new DirectoryInfo(destFileName);
                if (!Directory.Exists(dirInfo.Parent.FullName))
                {
                    Directory.CreateDirectory(dirInfo.Parent.FullName);
                }
                File.Copy(sourceFileName, destFileName);
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }

        public static bool CopyFile(string sourceDir, string sourceFileName, string destFileName)
        {
            try
            {
                if (!Directory.Exists(sourceDir))
                {
                    Directory.CreateDirectory(sourceDir);
                }

                File.Copy(sourceDir + "\\" + sourceFileName, sourceDir + "\\" + destFileName);
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }

        public static bool DeleteFile(string fileName)
        {
            if (!File.Exists(fileName))
            {
                return true;
            }
            try
            {
                File.Delete(fileName);
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
    }
}
